<?php
abstract class UserActiveRecord extends CActiveRecord
{
    protected function beforeSave()
    {
        if(null !== Yii::app()->user){
            $id = Yii::app()->user->id;
        }else{
            $id = 1;
        }

        if($this->isNewRecord)
        {
            if($this->tableName() === '{{article}}')
            {
                $this->article = CHtml::encode($this->article);
                $this->description = mb_substr($this->article,0,200,'utf8');
            }
            if($this->tableName() === '{{user}}' && Yii::app()->controller->id === 'frontend' && Yii::app()->controller->action->id === 'registration')
            {
                $this->role = 'user';
                $this->password = md5($this->password);
                $this->avatar = '/images/avatar/noavatar.png';
            }
            if($this->tableName() === '{{user}}' && Yii::app()->controller->id === 'backend' && Yii::app()->controller->action->id === 'create')
            {
                $this->password = md5($this->password);
                if(!$this->avatar)
                {
                    $this->avatar = 'noavatar.png';
                }
            }

            if($this->tableName() === '{{user}}' &&  Yii::app()->controller->id === 'Backend' && !isset($this->avatar))
            {
                $this->avatar = Yii::app()->getModule('user')->params->defaultAvatar;
            }
            $this->create_user_id = $id;
        }
        else
        {
            if(!Yii::app()->user->isGuest)
            {
                $this->update_user_id = $id;
            }

            if($this->tableName() === '{{user}}' && !Yii::app()->user->isGuest)
            {
                //if(strlen($this->avatar) < 14)
                //{
                //    $this->avatar = '/images/avatar/noavatar.png';
                //}

                //Если обновляем пользовательские данные
               // if(isset($this->password) && strlen($this->password) < 32 )
                //{
                  //  $this->password = md5($this->password);
                //}
            }

            if(Yii::app()->controller->action->id === 'recovery' )
            {

                $this->password = md5($this->password);
                $this->update_time = time();
            }

        }
        return parent::beforeSave();

    }
    public function beforeDelete()
    {
        if($this->tableName() === '{{user}}' && $this->id ==1)
        {
            throw new CException(Yii::t('UserModule.base','Super Admin can not remove'));
        }
        if($this->avatar !== Yii::app()->getModule('user')->params->defaultAvatar)
        {
            $file_path = Yii::getPathOfAlias('application.modules.user.images.avatar').DIRECTORY_SEPARATOR.$this->avatar;
            if(file_exists($file_path))
            {
                @unlink($file_path);
            }
        }

        return parent::beforeDelete();
    }


    public function behaviors()
    {
        return array(
            'CTimestampBehavior' => array(
                'class'=>'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'create_time',
                'updateAttribute' => 'update_time',
                'setUpdateOnCreate' => true,
            )
        );
    }

}